﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameMgr : MonoBehaviour
{
    public static GameMgr Instance()
    {
        return GameMgr._instance;
    }
    private static GameMgr _instance;

    public Sprite[] Images;
    private GameObject[] back;
    private Tube[] tudes;
    private BirdController bird;
    private Text score;

    private float backgroundSize = 288 / 50;
    private int oldIndex;
    private float buildTubeX;

    private bool IsGameStart;
    private bool IsGameOver;
    private int Score;
    // Use this for initialization
    void Start()
    {
        GameMgr._instance = this;

        {
            this.score = GameObject.Find("GameUI/Score").GetComponent<Text>();
        }

        {
            GameObject o = Resources.Load<GameObject>("Prefab/Bird") as GameObject;
            this.bird = GameObject.Instantiate(o).GetComponent<BirdController>();
        }

        {
            var o = Resources.Load<GameObject>("Prefab/Background");

            this.back = new GameObject[3];
            this.back[0] = GameObject.Instantiate(o) as GameObject;
            this.back[1] = GameObject.Instantiate(o) as GameObject;
            this.back[2] = GameObject.Instantiate(o) as GameObject;

            this.back[0].transform.SetParent(this.transform);
            this.back[1].transform.SetParent(this.transform);
            this.back[2].transform.SetParent(this.transform);

            this.back[0].GetComponent<SpriteRenderer>().sprite = this.Images[0];
            this.back[1].GetComponent<SpriteRenderer>().sprite = this.Images[0];
            this.back[2].GetComponent<SpriteRenderer>().sprite = this.Images[0];
        }

        {
            var o = Resources.Load<GameObject>("Prefab/Tube");

            this.tudes = new Tube[5];
            for (int i = 0; i < this.tudes.Length; i++)
            {
                this.tudes[i] = GameObject.Instantiate(o).GetComponent<Tube>();
            }
        }

        this.GamePrepare();
    }

    public BirdController Bird()
    {
        return this.bird;
    }


    private void BuildTube(Tube tube)
    {
        this.buildTubeX = this.buildTubeX + Random.Range(4, 5);
        tube.transform.position = new Vector3(this.buildTubeX, 0, 0);
        tube.RandBuild(2, 3);
        tube.InUse = true;
        tube.gameObject.SetActive(true);
    }

    public void GamePrepare()
    {
        IsGameStart = false;
        IsGameOver = false;
        Score = 0;
        buildTubeX = 0;
        Physics2D.gravity = new Vector3(0, -10, 0);
        this.oldIndex = 0;
        this.score.text = "0";

        GameObject over = GameObject.Find("GameUI").transform.Find("GameOver").gameObject;
        over.SetActive(false);

        this.bird.transform.position = new Vector3(0, 0, 0);
        for (int i = 0; i < this.tudes.Length; i++)
        {
            this.tudes[i].InUse = false;
            this.tudes[i].gameObject.SetActive(false);
        }
        this.back[0].transform.localPosition = new Vector3(this.backgroundSize * (oldIndex), 0, 0);
        this.back[1].transform.localPosition = new Vector3(this.backgroundSize * (oldIndex + 1), 0, 0);
        this.back[2].transform.localPosition = new Vector3(this.backgroundSize * (oldIndex + 2), 0, 0);

        this.bird.Prepare();
    }

    public void GameOver()
    {
        if (this.IsGameOver == true)
        {
            return;
        }
        this.IsGameOver = true;
        this.IsGameStart = false;
        Debug.Log("GameOver");
        GameObject over = GameObject.Find("GameUI").transform.Find("GameOver").gameObject;
        over.SetActive(true);
        over.GetComponent<Animator>().Play("ShowGameOver");
    }

    // Update is called once per frame
    void Update()
    {
        bool keyDown = false;

        if (Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.Space))
        {
            keyDown = true;
            Debug.Log(Input.mousePosition);
        }

        if (this.IsGameOver)
        {
            if (keyDown)
            {
                this.GamePrepare();
            }
            return;
        }

        int index = (int)(this.bird.transform.localPosition.x / this.backgroundSize);

        if (index != oldIndex)
        {
            this.oldIndex = index;
            GameObject go = this.back[0];
            go.transform.localPosition = new Vector3(this.backgroundSize * (index + 2), 0, 0);
            this.back[0] = this.back[1];
            this.back[1] = this.back[2];
            this.back[2] = go;
        }

        if (IsGameStart == false)
        {
            if (keyDown)
            {
                IsGameStart = true;
                this.bird.GameStart();
                this.buildTubeX = this.bird.transform.position.x + 5;
                for (int i = 0; i < this.tudes.Length; i++)
                {
                    this.BuildTube(this.tudes[i]);
                }
            }
            else
            {
                return;
            }
        }

        if (keyDown)
        {
            this.bird.Jump();
        }

        for (int i = 0; i < this.tudes.Length; i++)
        {
            if (this.tudes[i].InUse)
            {
                if (this.bird.transform.position.x > this.tudes[i].transform.position.x + 0.5)
                {
                    this.tudes[i].InUse = false;
                    Score++;
                    this.score.text = Score.ToString();
                    Debug.Log("得分！！ " + Score);
                }
            }
            else
            {
                if (this.tudes[i].gameObject.activeSelf && this.bird.transform.position.x > this.tudes[i].transform.position.x + 5)
                {
                    BuildTube(this.tudes[i]);
                }
            }
        }

    }

}
